using System;
using AudioStudio;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.PlayerController
{
    public class PlayerAnimInput : MonoBehaviour
    {
        [SerializeField]
        private bool active = true;
        public bool Active
        {
            get => active;
            set => active = value;
        }
        
        public PlayerInput playerInput;

        public bool isSprint;
        public bool isDodge;
        
        public Animator animator;
        
        public string animHorName = "WalkH";
        private int _animHorId;
        
        public string animVerName = "WalkV";
        private int _animVerId;

        public string animSpeedName = "Speed";
        private int _animSpeedId;

        public string animDodgeSprintName = "dodgeSprint";
        public int _animDodgeSprintId;

        public string animDodgeName = "dodge";
        public int _animDodgeId;
        
        public string animGroundName = "Grounded";
        private int _animGroundId;
        private float soundTime;
        private void Awake()
        {
            _animHorId = Animator.StringToHash(animHorName);
            _animVerId = Animator.StringToHash(animVerName);

            _animSpeedId = Animator.StringToHash(animSpeedName);
            _animDodgeSprintId = Animator.StringToHash(animDodgeSprintName);
            _animDodgeId = Animator.StringToHash(animDodgeName);
            _animGroundId = Animator.StringToHash(animGroundName);
        }

        private void Update()
        {
            if(!Active) return;
            
            animator.SetFloat(_animHorId,playerInput.HorRealValue);
            animator.SetFloat(_animVerId, playerInput.VerRealValue);

            if (!isDodge && playerInput.IsDodge)
            {
                animator.SetBool(_animDodgeId,true);
            }
            else if (isDodge && !playerInput.IsDodge)
            {
                animator.SetBool(_animDodgeId,false);
            }
            isDodge = playerInput.IsDodge;
            
            if (!isSprint && playerInput.IsSprint)
            {
                // 冲刺
                animator.SetBool(_animDodgeSprintId, true);
                //dodge_sprint
            }
            else if (isSprint && !playerInput.IsSprint)
            {
                // 取消冲刺
                animator.SetBool(_animDodgeSprintId, false);

            }
            isSprint = playerInput.IsSprint;
        }

        public void SetAnimTrigger(string tName)
        {
            animator.ResetTrigger(tName);
            animator.SetTrigger(tName);
        }

        public void SetSpeed(float speed)
        {
            if(!Active) return;

            if (speed > 0.1f)
            {
                soundTime += Time.deltaTime;
                if (soundTime > 0.3f)
                {
                    AudioManager.PlaySound("am_street_npc_run_step",animator.gameObject);
                    soundTime = 0;
                }
            }
            animator.SetFloat(_animSpeedId,speed);
        }

        public void SetGrounded(bool grounded)
        {
            if(!Active) return;
            
            animator.SetBool(_animGroundId,grounded);
        }
    }
}